import { Controller, Get, Request } from '@nestjs/common';
import { CosStsService } from './cos.service';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { ApiException } from 'src/core/exceptions/api.exception';

@ApiTags('COS模块')
@ApiBearerAuth()
@Controller('cos')
export class CosController {
  constructor(private readonly cosStsService: CosStsService) {}

  @Get('credential')
  @ApiOperation({ summary: '获取临时密钥' })
  async getCredential(@Request() req) {
    const { path, ext } = req.query;
    if (!['jpg', 'jpeg', 'png', 'docx', 'doc', 'pdf'].includes(ext)) {
      throw new ApiException(10002);
    }
    if (!['resume', 'avatar', 'company', 'logo', 'banner'].includes(path)) {
      throw new ApiException(10003);
    }
    return await this.cosStsService.getStsCredentials(ext, path);
  }

  @Get('delete')
  @ApiOperation({ summary: '移除文件' })
  removeCosFile(@Request() req) {
    const { key, path } = req.query;
    const cosKey = path.substr(path.indexOf(key));
    return this.cosStsService.deleteObject(cosKey);
  }
}
